package com.ruoyi.loss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.loss.domain.bo.WellLostDescribeBo;
import com.ruoyi.loss.domain.vo.WellLostDescribeVo;
import com.ruoyi.loss.domain.WellLostDescribe;
import com.ruoyi.loss.mapper.WellLostDescribeMapper;
import com.ruoyi.loss.service.IWellLostDescribeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 本井漏失描述Service业务层处理
 *
 * @author toteny
 * @date 2024-01-02
 */
@RequiredArgsConstructor
@Service
public class WellLostDescribeServiceImpl implements IWellLostDescribeService {

    private final WellLostDescribeMapper baseMapper;

    /**
     * 查询本井漏失描述
     */
    @Override
    public WellLostDescribeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询本井漏失描述列表
     */
    @Override
    public TableDataInfo<WellLostDescribeVo> queryPageList(WellLostDescribeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WellLostDescribe> lqw = buildQueryWrapper(bo);
        Page<WellLostDescribeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询本井漏失描述列表
     */
    @Override
    public List<WellLostDescribeVo> queryList(WellLostDescribeBo bo) {
        LambdaQueryWrapper<WellLostDescribe> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WellLostDescribe> buildQueryWrapper(WellLostDescribeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WellLostDescribe> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWellId()), WellLostDescribe::getWellId, bo.getWellId());
        lqw.eq(StringUtils.isNotBlank(bo.getWellNo()), WellLostDescribe::getWellNo, bo.getWellNo());
        lqw.eq(StringUtils.isNotBlank(bo.getWellLayer()), WellLostDescribe::getWellLayer, bo.getWellLayer());
        lqw.eq(StringUtils.isNotBlank(bo.getWellHorizon()), WellLostDescribe::getWellHorizon, bo.getWellHorizon());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateDept()), WellLostDescribe::getCreateDept, bo.getCreateDept());
        return lqw;
    }

    /**
     * 新增本井漏失描述
     */
    @Override
    public Boolean insertByBo(WellLostDescribeBo bo) {
        WellLostDescribe add = BeanUtil.toBean(bo, WellLostDescribe.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改本井漏失描述
     */
    @Override
    public Boolean updateByBo(WellLostDescribeBo bo) {
        WellLostDescribe update = BeanUtil.toBean(bo, WellLostDescribe.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WellLostDescribe entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除本井漏失描述
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
